package org.tobaccomain.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.tobaccomain.mapper.DeskMapper;
import org.tobaccomain.mapper.YearMapper;
import org.tobaccomain.pojo.dto.RfidDTO;
import org.tobaccomain.pojo.entity.LocationData;
import org.tobaccomain.pojo.entity.OverviewData;
import org.tobaccomain.pojo.entity.Rfid;
import org.tobaccomain.pojo.entity.YearData;
import org.tobaccomain.service.DeskService;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class DeskServiceImpl implements DeskService {

    @Autowired
    private DeskMapper deskMapper;
    @Autowired
    private YearMapper yearMapper;

    //获取全部的rfid号
    @Override
    public List<RfidDTO> getAllRfidList() {
        return deskMapper.selectAllRfids();
    }

    //获取可用的rfid号
    @Override
    public List<RfidDTO> getAvailableRfidList() {
        return deskMapper.selectAvailableRfids();
    }

    //rfid数据上传
    @Override
    public void uploadRfid(MultipartFile file) throws IOException {
        XSSFWorkbook excel = new XSSFWorkbook(file.getInputStream());
        XSSFSheet sheet = excel.getSheetAt(0);
        int rowNum = sheet.getLastRowNum();
        //定义列表存储导入的数据
        List<Rfid> list = new ArrayList<>();
        for (int i = 1; i <= rowNum ; i++) {
            Rfid rfid = new Rfid();
            XSSFRow row = sheet.getRow(i);
            rfid.setValue(Long.valueOf(row.getCell(0).getStringCellValue()));
            rfid.setTag((int) row.getCell(1).getNumericCellValue());
            list.add(rfid);
        }
        //关闭资源
        excel.close();
        deskMapper.unloadRfid(list);
    }

    //获取概览数据
    @Override
    public OverviewData getOverview() {
        OverviewData overviewData = deskMapper.selectOverview();
        //获取产地个数
        int number = deskMapper.selectOverviewOfLocation();
        overviewData.setLocation(number);
        return overviewData;
    }

    //获取单料烟年份数据
    @Override
    public List<YearData> getYearData() {
        QueryWrapper<YearData> wrapper = new QueryWrapper<YearData>();
        wrapper.orderByAsc("year");
        List<YearData> list = yearMapper.selectList(wrapper);
        return list;
    }

    //获取各个产地的单料烟数量
    @Override
    public List<LocationData> getNumberOfLocation() {
        List<LocationData> list = deskMapper.selectNumberOfLocation();
        return list;
    }

}
